package io.bdmc.modules.bss.controller;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.File;
import java.io.FileOutputStream;

import io.bdmc.common.utils.Constant;
import io.bdmc.common.utils.DateUtils;
import io.bdmc.common.utils.ExcelImport;
import io.bdmc.common.utils.MyMap;
import io.bdmc.common.utils.MyPage;
import io.bdmc.common.utils.R;
import io.bdmc.modules.bss.entity.DeviceAlarm;
import io.bdmc.modules.bss.model.DeviceAlarmParam;
import io.bdmc.modules.bss.service.IDeviceAlarmService;
import io.bdmc.modules.sys.controller.AbstractController;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellStyle;

/**
 * <p>
 * 设备报警 前端控制器
 * </p>
 *
 * @author ping
 * @since 2020-07-01
 */

// "设备报警历史记录", description = "/alarm/history")
@RestController
@RequestMapping("bss/alarm/history")
public class DeviceAlarmController extends AbstractController {

	@Autowired
	IDeviceAlarmService _devicealarmSvs;

	@GetMapping("/list")
	// "分页查询报警信息", notes = "分页查询报警信息")
	public R list(DeviceAlarmParam params) {
		MyPage<DeviceAlarm> myPage = new MyPage<DeviceAlarm>(params.getCurrent(), params.getSize());
		MyPage<DeviceAlarm> map = _devicealarmSvs.mySelectPage(myPage, params);
		return R.ok().putData(MyMap.ok(map.getRecords(), map.getTotal()));

	}

	@RequestMapping(value = "export")
	public R export(DeviceAlarmParam params) throws IOException {
		HSSFWorkbook workbook = new HSSFWorkbook();
		HSSFSheet sheet = workbook.createSheet("设备历史告警记录");
		MyPage<DeviceAlarm> myPage = new MyPage<DeviceAlarm>(1, 30000);
		MyPage<DeviceAlarm> map = _devicealarmSvs.mySelectPage(myPage, params);
		List<DeviceAlarm> list = map.getRecords();
		String dateString = DateUtils.formatLocalDateTime(LocalDateTime.now(), DateUtils.Pay_DATE_TIME_PATTERN);
		String fileName = "设备历史告警记录" + dateString + ".xls";// 设置要导出的文件的名字

		Map<String, CellStyle> styles = ExcelImport.createStyles(workbook);

		// 新增数据行，并且设置单元格数据
		int rowNum = 0;
		String[] headers = { "序号", "设备名称", "设备型号", "端口", "告警名称", "告警编码", "告警级别", "告警信息", "确认状态", "确认用户", "开始时间",
				"最后告警时间", "可能原因", "处理方法", };

		rowNum = ExcelImport.initialize(rowNum, "设备历史告警记录", headers, workbook, sheet, styles);
		// 在表中存放查询到的数据放入对应的列
		int dataIndex = 1;
		for (DeviceAlarm dd : list) {
			HSSFRow row1 = sheet.createRow(rowNum++);
			row1.createCell(0).setCellValue(dataIndex);
			row1.createCell(1).setCellValue(dd.getDeviceName());
			row1.createCell(2).setCellValue(ExcelImport.convertNullString(dd.getDeviceModel()));
			row1.createCell(3).setCellValue(ExcelImport.convertNullString(dd.getChannel()));
			row1.createCell(4).setCellValue(ExcelImport.convertNullString(dd.getAlarmTypeName()));
			row1.createCell(5).setCellValue(ExcelImport.convertNullString(dd.getAlarmTypeCode()));
			row1.createCell(6).setCellValue(ExcelImport.convertNullString(getAlarmLevelString(dd.getAlarmLevel())));
			row1.createCell(7).setCellValue(ExcelImport.convertNullString(dd.getAlarmMsg()));
			row1.createCell(8).setCellValue(ExcelImport.convertNullString(dd.getProcessed() ? "已确认" : "未确认"));
			row1.createCell(9).setCellValue(ExcelImport.convertNullString(dd.getProcessBy()));
			row1.createCell(10).setCellValue(
					ExcelImport.convertNullString(DateUtils.formatLocalDateTimeDefault(dd.getStartTime())));
			row1.createCell(11)
					.setCellValue(ExcelImport.convertNullString(DateUtils.formatLocalDateTimeDefault(dd.getEndTime())));
			row1.createCell(12).setCellValue(ExcelImport.convertNullString(dd.getPossibleCause()));
			row1.createCell(13).setCellValue(ExcelImport.convertNullString(dd.getProcessMethod()));
			dataIndex++;
		}
		try {
			File path = new File(Constant.EXPORTPATH);
			if (!path.exists()) {
				path.mkdir();
			}
			File f = new File(path.getAbsolutePath() + "/excel/", fileName);
			if (!f.getParentFile().exists()) {
				f.getParentFile().mkdir();
			}
			FileOutputStream fileOutputStream = new FileOutputStream(f);// 指定路径与名字和格式
			workbook.write(fileOutputStream);// 将数据写出去
			fileOutputStream.close();// 关闭输出流
			return R.ok().putData("/excel/" + fileName);
		} catch (Exception e) {
			e.printStackTrace();
			return R.error(-1, e.getMessage());
		}
	}

	private String getAlarmLevelString(String alarmLevel) {
		String str = "";
		switch (alarmLevel) {
			case "A":
				str = "紧急告警";
				break;
			case "B":
				str = "主要告警";
				break;
			case "C":
				str = "次要告警";
				break;
			case "D":
				str = "提示告警";
				break;
			default:
				break;
		}
		return str;
	}

	@PostMapping("/delete")
	// @RequiresPermissions("bss:alarm:history:delete")
	// "删除所有报警信息", notes = "删除所有报警信息")
	public R delete() {
		boolean result = _devicealarmSvs.remove(new QueryWrapper<>());
		return R.trueOrFalse(result);

	}

}
